package cn.danger.com.fragment.schedule;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import cn.danger.com.activity.CommonActivity;
import cn.danger.com.activity.R;
import cn.danger.com.fragment.BackHandledFragment;

/**
 * Created by 俊 on 2017/1/3.
 */
public class TypeFragment extends BackHandledFragment implements View.OnClickListener {

    private boolean hadIntercept;
    private View view;
    private CommonActivity activity;
    private Toolbar toolbar;
    private RelativeLayout work_layout;
    private ImageView work_imageView;
    private RelativeLayout route_layout;
    private ImageView route_imageView;
    private RelativeLayout life_layout;
    private ImageView life_imageView;
    private Handler handler;
    private String type = "工作";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_type, container, false);

        bindView();
        bindEvent();

        return view;
    }

    @Override
    public void bindView() {
        activity = (CommonActivity) getActivity();
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        toolbar.setTitle("");
        activity.setSupportActionBar(toolbar);

        work_layout = (RelativeLayout) view.findViewById(R.id.work_layout);
        work_imageView = (ImageView) view.findViewById(R.id.work_imageView);
        route_layout = (RelativeLayout) view.findViewById(R.id.route_layout);
        route_imageView = (ImageView) view.findViewById(R.id.route_imageView);
        life_layout = (RelativeLayout) view.findViewById(R.id.life_layout);
        life_imageView = (ImageView) view.findViewById(R.id.life_imageView);

        Bundle bundle = getArguments();

        if (bundle != null) {
            type = bundle.getString("index");
            reset();
            if (type.equals("工作")) {
                work_imageView.setVisibility(View.VISIBLE);
            } else if (type.equals("行程")) {
                route_imageView.setVisibility(View.VISIBLE);
            } else if (type.equals("生活")) {
                life_imageView.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void bindEvent() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                back();
            }
        });
        work_layout.setOnClickListener(this);
        route_layout.setOnClickListener(this);
        life_layout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        reset();
        switch (v.getId()) {
            case R.id.work_layout:
                work_imageView.setVisibility(View.VISIBLE);
                type = "工作";
                break;
            case R.id.route_layout:
                route_imageView.setVisibility(View.VISIBLE);
                type = "行程";
                break;
            case R.id.life_layout:
                life_imageView.setVisibility(View.VISIBLE);
                type = "生活";
                break;
            default:
                break;
        }
        back();
    }

    private void back(){
        if (handler != null) {
            Message message = Message.obtain();
            message.what = 3;
            message.obj = type;
            handler.sendMessage(message);
        }
        getFragmentManager().popBackStack();
    }

    @Override
    public boolean onBackPressed() {
        if (hadIntercept) {
            return false;
        } else {
            hadIntercept = true;
            return true;
        }
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    private void reset() {
        work_imageView.setVisibility(View.GONE);
        route_imageView.setVisibility(View.GONE);
        life_imageView.setVisibility(View.GONE);
    }

}